package com.smarth.zhizulian.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.activity.AgentWebActivity;
import com.smarth.zhizulian.activity.WebViewActivity;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.bean.Category;
import com.smarth.zhizulian.bean.video.VideoVideoTexts;
import com.smarth.zhizulian.utils.ACache;
import com.smarth.zhizulian.utils.GlideUtils;
import com.smarth.zhizulian.utils.GsonUtils;
import com.smarth.zhizulian.utils.ViewHolder;
import com.smarth.zhizulian.utils.listhelper.impl.adapter.ListDataAdapter;

import java.util.List;

/**
 * Created by 18865 on 2018/1/24.
 */

public class VideoAdapter extends ListDataAdapter {
    private Category mCategory;

    public VideoAdapter(Context context, List datas, Category category) {
        super(context, datas);
        this.mCategory = category;
    }

    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        convertView = getConvertView(convertView, R.layout.item_video);
        TextView tv_title = ViewHolder.get(convertView, R.id.tv_title);
        TextView tv_time = ViewHolder.get(convertView, R.id.tv_time);
        TextView tv_coin = ViewHolder.get(convertView, R.id.tv_coin);
        TextView tv_playcount = ViewHolder.get(convertView, R.id.tv_playcount);
        ImageView iv_video = ViewHolder.get(convertView, R.id.iv_video);

        final VideoVideoTexts vvt = (VideoVideoTexts) mDatas.get(position);
        tv_title.setText(vvt.getTitle());
        tv_time.setText(vvt.getVideoDuration());
        tv_coin.setText("" + vvt.getCoin());
        tv_playcount.setText("播放" + vvt.getReadCount() + "次");
        GlideUtils.loadVideo(mContext, vvt.getImage(), iv_video);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AgentWebActivity.openForVideo(mContext, "" + vvt.getRelationId());
//                WebViewActivity.openForVideo(mContext, "test", "" + vvt.getRelationId());
            }
        });
        return convertView;
    }

    @Override
    public void onGetData(List profitProfits, boolean isRefresh) {
        super.onGetData(profitProfits, isRefresh);
        if (isRefresh) {
            ACache.get(mContext).put(BaseConfig.ACache.VIDEO + mCategory.getName(), GsonUtils.toJson(profitProfits));
        }
    }
}
